package com.deco.back.controller.sys;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.deco.domain.vo.sys.ChinastatesVo;
import com.deco.service.sys.ChinastatesService;
import com.free.common.vo.ResultModel;
	
@RestController
public class ChinastatesController {
	
	@Autowired
	private ChinastatesService chinastatesService;
	
	
	@ResponseBody
    @RequestMapping(value = "ajaxGetAreaMap")
    public ResultModel ajaxGetAreaMap(HttpServletRequest request, HttpServletResponse response,String code,String type){

		 Map<String, ChinastatesVo> map = new LinkedHashMap<String, ChinastatesVo>(); 
	        if(StringUtils.isNotBlank(code) && StringUtils.isNotBlank(type)){
	            Map<String, ChinastatesVo> areaMap = chinastatesService.getAll();
	            if("city".equals(type)){
	            	ChinastatesVo prov = areaMap.get(code);
	                if(prov != null){
	                    map = prov.getChildMap();
	                }
	            } else {
	                String provStr = code.substring(0, 2);
	                ChinastatesVo prov = areaMap.get(provStr);
	                if(prov != null){
	                    map = prov.getChildMap();
	                    ChinastatesVo city = map.get(code);
	                    map = city.getChildMap();
	                }
	            }
	        }
	        return ResultModel.success(map);
    }
}
